package ui.user;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import tool.MyButton;
import vo.UserVo;
import vo.Vo;
import blservice.user.UserBLService;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;

import javax.swing.JScrollPane;

public class UserManagementPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private UserBLService userBL;
	private UserVo user;
	private JTable table;
	private JScrollPane tablePane;
	private List<UserVo> users;
	
	public UserManagementPanel(UserVo user){
		try {
			userBL=BLServiceManager.getUserBLService();
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this,e1.getMessage());
			e1.printStackTrace();
			return;
		}
		
		this.user=user;
		this.setLayout(null);
		this.setOpaque(false);
		this.setVisible(true);
		
		JButton btnNewButton = new MyButton("Graphics/Button86-25.png");
		btnNewButton.setText("\u589E\u52A0\u7528\u6237\n");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AddDialog(UserManagementPanel.this,user, userBL).setVisible(true);
			}
		});
		btnNewButton.setBounds(72, 376, 86, 25);
		add(btnNewButton);
		
		JButton button = new MyButton("Graphics/Button86-25.png");
		button.setText("\u5220\u9664\u7528\u6237");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=table.getSelectedRow();
				if(row!=-1){
					deleteUser((String)table.getValueAt(row, 0));
				}
			}
		});
		button.setBounds(282, 376, 86, 25);
		add(button);
		
		JButton button_1 = new MyButton("Graphics/Button86-25.png");
		button_1.setText("\u4FEE\u6539\u6743\u9650");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=table.getSelectedRow();
				if(row!=-1){
					new UpdateDialog(UserManagementPanel.this,user,userBL,users.get(row)).setVisible(true);;
				}
				
			}
		});
		
		button_1.setBounds(493, 376, 86, 25);
		add(button_1);
		
		table=new JTable(){
			private static final long serialVersionUID = -4259670773587453305L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		tablePane = new JScrollPane(table);
		tablePane.setBounds(72, 41, 507, 304);
		add(tablePane);
		
		showAll();
	}
	
	

	private void deleteUser(String name){
		try {
			UserVo vo=new UserVo();
			vo.setOperator(user.getName());
			vo.setOperation("删除用户");
			vo.setName(name);
			Vo result=userBL.deleteUser(vo);
			if(result.isSuccessed()){
				JOptionPane.showMessageDialog(this,
						"删除用户成功\n" );
				showAll();
			}else{
				JOptionPane.showMessageDialog(this,
						"删除用户失败\n" + result.getFailMessage());
			}
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}
	
	
	
	void showAll(){
		try {
			users=userBL.getAll();
			
			table.setModel(new DefaultTableModel(
				readUserVo(users),
				new String[] {
					"姓名", "类型", "权限"
				}
			));
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	private String[][] readUserVo(List<UserVo> user){
		String[][] result=new String[user.size()][3];
		for(int i=0;i<result.length;i++){
			result[i][0]=user.get(i).getName();
			result[i][1]=user.get(i).getUserType().name();
			result[i][2]=user.get(i).getAuthority().name();
		}
		return result;
	}
}
